package com.wortox.setPrice;

import com.wortox.pojo.Fruit;

import java.util.ArrayList;
import java.util.List;

public class FruitPrice {

    //定义一个静态的成员变量,用来存储创建的该类的唯一对象
    private static FruitPrice fruitPrice;
    public static List<Fruit> fruitList;

    static {
        fruitList = new ArrayList<>();
        Fruit fruit1 = new Fruit();
        fruit1.setName("apple");
        fruit1.setPrice(800);
        Fruit fruit2 = new Fruit();
        fruit2.setName("strawberry");
        fruit2.setPrice(1300);
        fruitList.add(fruit1);
        fruitList.add(fruit2);
    }

    //构造方法私有化
    private FruitPrice(){}

    public static synchronized FruitPrice getInstance() {
        // 判断
        // 如果是第一次调用该方法,就创建该类的对象
        if (fruitPrice == null) {
            fruitPrice = new FruitPrice();
        }
        // 如果不是第一次调用该方法,就返回第一次调用的时候创建的对象
        return fruitPrice;
    }

    public List<Fruit> getFruitPrice(){
        return fruitList;
    }
}
